<?php

function export($nama_file, $judul, $header, $data, $jml_kolom, $jml_record)
{
	$namaFile = $nama_file.".xls";

	// Function penanda awal file (Begin Of File) Excel

	function xlsBOF() {
	echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
	return;
	}

	// Function penanda akhir file (End Of File) Excel

	function xlsEOF() {
	echo pack("ss", 0x0A, 0x00);
	return;
	}

	// Function untuk menulis data (angka) ke cell excel

	function xlsWriteNumber($Row, $Col, $Value) {
	echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
	echo pack("d", $Value);
	return;
	}

	// Function untuk menulis data (text) ke cell excel

	function xlsWriteLabel($Row, $Col, $Value ) {
	$L = strlen($Value);
	echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
	echo $Value;
	return;
	}

	// header file excel

	header("Pragma: public");
	header("Expires: 0");
	header("Cache-Control: must-revalidate, post-check=0,pre-check=0");
	header("Content-Type: application/force-download");
	header("Content-Type: application/octet-stream");
	header("Content-Type: application/download");

	// header untuk nama file
	header("Content-Disposition: attachment;
			filename=".$namaFile."");

	header("Content-Transfer-Encoding: binary ");

	// memanggil function penanda awal file excel
	xlsBOF();

	xlsWriteLabel(0,0,$judul);

	for($i=0;$i<count($header);$i++)
		xlsWriteLabel(3,$i,$header[$i]);

	$noBarisCell = 4;

	for($i=0;$i<count($data);$i++)
	{
		for($j=0;$j<$jml_kolom;$j++)
		{
                        if(is_numeric($data[$i][$j])){
                           xlsWriteNumber($noBarisCell,$j,$data[$i][$j]);    
                        }else{
			  xlsWriteLabel($noBarisCell,$j,$data[$i][$j]);
                        }  
		}
		$noBarisCell++;
	}

	// memanggil function penanda akhir file excel
	xlsEOF();
	exit();
}

?>